package severlet;

import bean.LeaveReturn;
import dao.LeaveReturnDao;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(name = "StudentLeavereturnSubmitServlet", value = "/StudentLeavereturnSubmitServlet")
public class StudentLeavereturnSubmitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8"); // 设置编码格式
        request.setCharacterEncoding("utf-8");

        String studentname=(String) request.getSession().getAttribute("studentname");
        String dormitoryid = request.getParameter("dormitoryid");
        Date leavetime=null;
        Date returntime=null;
        try {
            //将前面数出来的字符串形式转化为data形式         输入格式必须为****-**-**
            leavetime=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("leavetime"));
            returntime=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("returntime"));

        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("=============输入时间有误==============");
        }
if(dormitoryid == null || leavetime==null || returntime==null){
    request.getRequestDispatcher("FanHui.jsp").forward(request,response);
}
        //封住
        LeaveReturn leaveReturn=new LeaveReturn();
        leaveReturn.setStudentname(studentname);
        leaveReturn.setDormitoryid(dormitoryid);
        leaveReturn.setLeavetime(leavetime);
        leaveReturn.setReturntime(returntime);
        //调用数据库
        LeaveReturnDao leaveReturnDao=new LeaveReturnDao();
        //提交
        leaveReturnDao.submit(leaveReturn);

//        request.getRequestDispatcher("student_leavereturn.jsp").forward(request,response);
        request.getRequestDispatcher("/StudentLeavereturnServlet").forward(request,response);

    }
}
